package com.cms.setting.friendlink.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.framework.utils.BeanUtilEx;
import com.cms.framework.utils.page.PageView;
import com.cms.setting.friendlink.dao.FriendlinkDAO;
import com.cms.setting.friendlink.pojo.Friendlink;
import com.cms.setting.friendlink.pojo.dto.FriendlinkDTO;
import com.cms.setting.friendlink.service.FriendlinkService;
@Service
public class FriendlinkServiceImpl implements FriendlinkService {
	@Autowired
	FriendlinkDAO friendlinkDAO;
	
	@Override
	public boolean saveFriendlink(Friendlink friendlink) {
		boolean flag=false;
		if(friendlink.getId()>0){
			flag=friendlinkDAO.updateByPrimaryKeySelective(friendlink)>0;
		}else{
			flag=friendlinkDAO.insert(friendlink)>0;
		}
		return flag;
	}

	@Override
	public PageView<FriendlinkDTO> findListByPage(
			PageView<FriendlinkDTO> pageView, FriendlinkDTO friendlinkDTOParams) {
		List<Friendlink> list=friendlinkDAO.selectByPage(pageView, friendlinkDTOParams);
		List<FriendlinkDTO> dtoList=new ArrayList<FriendlinkDTO>();
		for(Friendlink friendlink:list){
			FriendlinkDTO friendlinkDTO=new FriendlinkDTO();
			try {
				BeanUtilEx.copyProperties(friendlinkDTO, friendlink);
				//friendlinkDTO.setPcUrl("pcUrl");
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			dtoList.add(friendlinkDTO);
		}
		pageView.setRecords(dtoList);
		return pageView;
	}

	@Override
	public Friendlink findById(Integer id) {
		return friendlinkDAO.selectByPrimaryKey(id);
	}

	@Override
	public boolean deleteById(Integer id) {
		return friendlinkDAO.deleteByPrimaryKey(id)>0;
	}

	@Override
	public boolean hideOrShowWapById(Integer id) {
		boolean flag=false;
		Friendlink friendlink=friendlinkDAO.selectByPrimaryKey(id);
		if(friendlink.getWapShow()==0){
			friendlink.setWapShow(1);
		}else{
			friendlink.setWapShow(0);
		}
		flag=friendlinkDAO.updateByPrimaryKey(friendlink)>0;
		return flag;
	}

	@Override
	public boolean hideOrShowPcById(Integer id) {
		boolean flag=false;
		Friendlink friendlink=friendlinkDAO.selectByPrimaryKey(id);
		if(friendlink.getPcShow()==0){
			friendlink.setPcShow(1);
		}else{
			friendlink.setPcShow(0);
		}
		flag=friendlinkDAO.updateByPrimaryKey(friendlink)>0;
		return flag;
	}
	
	

}
